//
//  ViewController.m
//  testapp
//
//  Created by Heng Yue on 2019/10/18.
//  Copyright © 2019 Heng Yue. All rights reserved.
//

#import "ViewController.h"
#import<SystemConfiguration/CaptiveNetwork.h>
#import "YSPhotoBroser.h"
#import "Test.h"
#import "Peson.h"
#import "Test+TESTEX.h"
#import "FscrollView.h"
#import <ZFPlayer/ZFPlayer.h>
#import <ZFPlayer/ZFAVPlayerManager.h>
#import <ZFPlayer/ZFIJKPlayerManager.h>
//#import <ZFPlayer/KSMediaPlayerManager.h>
#import <ZFPlayer/ZFPlayerControlView.h>
#import <objc/runtime.h>
typedef void(^callback)(NSString*);
static NSString *kVideoCover = @"https://upload-images.jianshu.io/upload_images/635942-14593722fe3f0695.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240";
@interface ViewController ()<UINavigationControllerDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) ZFPlayerController *player;
@property (nonatomic, strong) UIImageView *containerView;
@property (nonatomic, strong) ZFPlayerControlView *controlView;
@property (nonatomic, strong) UIButton *playBtn;
@property(nonatomic,strong) NSMutableArray *data;
@property(nonatomic,strong) UICollectionView *collectionView;
@property(nonatomic,strong) UIScrollView *scrollview;
@property(nonatomic,assign) callback callback;
@property(nonatomic,assign) NSArray *arr;
@end

@implementation ViewController
+(void)load{
    /** 获取原始setBackgroundColor方法 */
    Method originalM = class_getInstanceMethod([self class], @selector(test:));
       
       /** 获取自定义的pb_setBackgroundColor方法 */
       Method exchangeM = class_getInstanceMethod([self class], @selector(run:));
       
       /** 交换方法 */
       method_exchangeImplementations(originalM, exchangeM);
}
-(void)test:(NSString*)sender{
    NSLog(@"%@  ---%@",sender,NSStringFromSelector(_cmd));
}
-(void)run:(NSString*)text{
    NSLog(@"%@",text);
}
-(void)tap:(id)sender{
    NSLog(@"person tap");
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self run:@"run method 123"];
//    for (int i = 0; i < 10e5 * 2; i++) {
//        Test *vc=[[Test alloc] init];
//        @autoreleasepool {
//            NSString *str = [NSString stringWithFormat:@"hi + %d", i];
//        }
//    }
//    NSLog(@"finished!");


}

- (UIImageView *)containerView {
    if (!_containerView) {
        _containerView = [UIImageView new];
//        [_containerView setImageWithURLString:kVideoCover placeholder:[ZFUtilities imageWithColor:[UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1] size:CGSizeMake(1, 1)]];
    }
    return _containerView;
}

- (UIButton *)playBtn {
    if (!_playBtn) {
        _playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_playBtn setImage:[UIImage imageNamed:@"new_allPlay_44x44_"] forState:UIControlStateNormal];
        [_playBtn addTarget:self action:@selector(playClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _playBtn;
}
- (void)playClick:(UIButton *)sender {
    [self.player playTheIndex:0];
    [self.controlView showTitle:@"视频标题" coverURLString:kVideoCover fullScreenMode:ZFFullScreenModeAutomatic];

    
}
- (UIStatusBarStyle)preferredStatusBarStyle {
    if (self.player.isFullScreen) {
        return UIStatusBarStyleLightContent;
    }
    return UIStatusBarStyleDefault;
}

- (BOOL)prefersStatusBarHidden {
    return self.player.isStatusBarHidden;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationSlide;
}

- (BOOL)shouldAutorotate {
    return self.player.shouldAutorotate;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (ZFPlayerControlView *)controlView {
    if (!_controlView) {
        _controlView = [ZFPlayerControlView new];
        _controlView.fastViewAnimated = YES;
        _controlView.autoHiddenTimeInterval = 5;
        _controlView.autoFadeTimeInterval = 0.5;
        _controlView.prepareShowLoading = YES;
        
        _controlView.prepareShowControlView = YES;
    }
    return _controlView;
}


- (void)viewDidLoad {
    
    NSNumber *temp=nil;
     NSMutableArray *arr=[NSMutableArray arrayWithObjects:@10,@20,@3,@14,@0,@5, nil];
    for (int i=0; i<arr.count; i++) {
        for (int j=i+1; j<arr.count; j++) {
            
            if ([arr[i] integerValue]<[arr[j] integerValue]) {
                temp=arr[j];
                arr[j]=arr[i];
                arr[i]=temp;
            }
        }
    }
    NSLog(@"%@",arr);
    Peson *a=[[Peson alloc] initWithFrame:CGRectMake(100, 300, 100, 60)];
    a.backgroundColor=[UIColor greenColor];
    [self.view addSubview:a];
  
     [self.view addSubview:self.containerView];
        
    [self.containerView addSubview:self.playBtn];
//        [self.view addSubview:self.changeBtn];
//        [self.view addSubview:self.nextBtn];

//        ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
////        ZFIJKPlayerManager *playerManager = [[ZFIJKPlayerManager alloc] init];
//
//        /// 播放器相关
//        self.player = [ZFPlayerController playerWithPlayerManager:playerManager containerView:self.containerView];
//        self.player.controlView = self.controlView;
//        /// 设置退到后台继续播放
//        self.player.pauseWhenAppResignActive = NO;
//
//        @weakify(self)
//        self.player.orientationWillChange = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
//            @strongify(self)
//            [self setNeedsStatusBarAppearanceUpdate];
//        };
//
//        /// 播放完成
//        self.player.playerDidToEnd = ^(id  _Nonnull asset) {
//            @strongify(self)
//            [self.player.currentPlayerManager replay];
//            [self.player playTheNext];
//            if (!self.player.isLastAssetURL) {
//                NSString *title = [NSString stringWithFormat:@"视频标题%zd",self.player.currentPlayIndex];
//                [self.controlView showTitle:title coverURLString:kVideoCover fullScreenMode:ZFFullScreenModeLandscape];
//            } else {
//                [self.player stop];
//            }
//        };
//
//    //    self.player.assetURLs = self.assetURLs;
////    NSString *path=[[NSBundle mainBundle] pathForResource:@"01" ofType:@"mp4"];
//        self.player.assetURL=[NSURL URLWithString:@"http://www.baidu.com"];
//    [self.controlView showTitle:@"" coverURLString:kVideoCover fullScreenMode:ZFFullScreenModeAutomatic];

    UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem];
    [btn setTitle:@"menu" forState:UIControlStateNormal];
    btn.frame=CGRectMake(100, 500, 100, 50);
    [self.view addSubview:btn];
    btn.backgroundColor=[UIColor grayColor];
    [btn addTarget:self action:@selector(menu:) forControlEvents:UIControlEventTouchUpInside];
 
    
}
-(void)menu:(UIButton*)sender{
    [self becomeFirstResponder];
    NSMutableArray *items =  [[NSMutableArray alloc] init];

    UIMenuItem *menuItem;
    menuItem = [[UIMenuItem alloc] initWithTitle:@"BookMark" action:@selector(book:)];
    [items addObject:menuItem];
  
    menuItem = [[UIMenuItem alloc] initWithTitle:@"Note" action:@selector(note:)];
    [items addObject:menuItem];
    [UIMenuController sharedMenuController].arrowDirection=UIMenuControllerArrowUp;
    [[UIMenuController sharedMenuController] setMenuItems:items];
   
    
    if (@available(iOS 13.0, *)) {
        [[UIMenuController sharedMenuController] showMenuFromView:self.view rect:sender.frame];
       
    } else {
        // Fallback on earlier versions
        [[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
           [[UIMenuController sharedMenuController] setTargetRect:sender.frame inView:self.view];
        
    }

}
-(BOOL)canBecomeFirstResponder{
    return YES;
}
//-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
//{
//    if (action == @selector(book:)) {
//        return YES;
//    }
//    if (action == @selector(note:)) {
//        return YES;
//    }
//    return NO;//隐藏系统默认的菜单项
//}
-(void)book:(id)sender{
    
}
-(void)note:(id)sender{
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 4;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//    cell.transform=CGAffineTransformMakeScale(0.9, 0.9);
    if (indexPath.item%2==0) {
        cell.backgroundColor=[UIColor redColor];
    }else{
          cell.backgroundColor=[UIColor groupTableViewBackgroundColor];
    }
    return cell;
}
- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    CGFloat x = 0;
    CGFloat y = CGRectGetMaxY(self.navigationController.navigationBar.frame);
    CGFloat w = CGRectGetWidth(self.view.frame);
    CGFloat h = w*9/16;
    self.containerView.frame = CGRectMake(x, y, w, h);
    
    w = 44;
    h = w;
    x = (CGRectGetWidth(self.containerView.frame)-w)/2;
    y = (CGRectGetHeight(self.containerView.frame)-h)/2;
    self.playBtn.frame = CGRectMake(x, y, w, h);
    
 
}
- (NSString *)ssid {
    NSString *ssid = nil;
    NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
    for (NSString *ifnam in ifs) {
        NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
        NSLog(@"dic===%@",info);
        if (info[@"SSID"]) {
            ssid = info[@"SSID"];
        }
    }
    return ssid;
}
//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    Test *vc=[[Test alloc] init];
//    [self.navigationController pushViewController:vc animated:YES];
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//    NSLog(@"remove notifacation");
//}
// 将要显示控制器隐藏导航 栏
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
//    self.navigationController.navigationBar.hidden=isShowHomePage;

}


@end
